package com.burgeon.backend.SHOP.rest;
/**
 * 积分商城预售定金
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOP.entity.presell.Earnest;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class CreateYC_YSRETAIL extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public CreateYC_YSRETAIL(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }
    /**
     * 发送并接收字符
     */
    public String getData(Earnest earnest) throws Exception {

        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(earnest), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        String message = new JSONArray(str).getJSONObject(0).getString("message");
        if (!"0".equals(code)) {
            throw new Exception(message);
        }
        return new JSONArray(str).getJSONObject(0).getString("objectid");
    }

    private MultiValueMap Data(Earnest earnest) throws Exception {

        HashMap params = new HashMap();
        HashMap masterobj = new HashMap(); //头表记录对象
        masterobj.put("table", "YC_YSRETAIL");
        masterobj.put("ID", "-1");
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateString = sdf.format(now);
        masterobj.put("BILLDATE", dateString);
        masterobj.put("XSNO", earnest.getOrderId());
        masterobj.put("RECEIVER_NAME",earnest.getReceiver());
        masterobj.put("RECEIVER_MOBILE",earnest.getRPhone());
        masterobj.put("RECEIVER_ADDRESS",earnest.getAddress());
        masterobj.put("CITY",earnest.getCity());
        masterobj.put("C_VIP_ID__CARDNO",earnest.getPhone());

        masterobj.put("TOT_AMTPAYED",earnest.getTpayed());
        masterobj.put("TOT_AMT_DEDUCTIBLE",earnest.getDeductible());
        masterobj.put("TOT_AMT_REM",earnest.getRem());

        masterobj.put("DESCRIPTION","'"+earnest.getComment()+"'");
        HashMap detailobjs = new HashMap(); //明细表记录对象
        ArrayList list = new ArrayList();
        FIndAD_Table fIndAD_table = new FIndAD_Table(ip,account,password);
        HashMap yc_retail = fIndAD_table.getData("YC_YSRETAIL");
        list.add(yc_retail.get("YC_YSRETAILITEM"));

        detailobjs.put("reftables", list);
//****************************************************************************
        ArrayList refobjs = new ArrayList();
        HashMap ref1 = new HashMap();
        ref1.put("table", "YC_YSRETAILITEM");//零售单明细

        List addList1 = new ArrayList();

            HashMap map1 = new HashMap();//addlist内容
            map1.put("id", "-1");
            map1.put("M_PRODUCT_ID__NAME", earnest.getCode());

            map1.put("QTY", "1");
            map1.put("PRICEACTUAL", earnest.getPrice());
            map1.put("TOT_AMT_ACTUAL",earnest.getPrice());//Double.parseDouble(data.get(2).toString())
            map1.put("TYPE", "1");
            map1.put("DESCRIPTION", "线上商城");
            map1.put("UNIQUEID",earnest.getOrderId());

            if (null!=earnest.getEmp()&&earnest.getEmp().length()!=0&&!"".equals(earnest.getEmp())){
                map1.put("SALESREP_ID__NAME",earnest.getEmp());
            }

            addList1.add(map1);


        ref1.put("addList", String.valueOf(addList1));

        refobjs.add(ref1);
        detailobjs.put("refobjs", refobjs);

        params.put("masterobj", masterobj);
        params.put("detailobjs", detailobjs);
        //params.put("submit", "true");//是否提交
        return DATA("ProcessOrder", params, account, password);
    }

    public static void main(String[] args) {
        CreateYC_YSRETAIL g = new CreateYC_YSRETAIL("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");

        Earnest earnest = new Earnest();
        earnest.setOrderId("1231222345666");
        earnest.setRem("20");
        earnest.setTpayed("10");
        earnest.setDeductible("20");
        earnest.setCode("BDN3HD5061W200");
        earnest.setPrice("30");
        earnest.setPhone("17732141036");
        try {
           System.err.println(g.getData(earnest));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
